package com.shiyi.design.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @author wangzhanshuai
 * @date 2021/2/5 8:21 下午
 * @since 1.0
 */
public class MyJdkProxy {

    private Movable movable;

    public MyJdkProxy(Movable movable) {
        this.movable = movable;
    }

    public Object getProxy() {
        ClassLoader classLoader = movable.getClass().getClassLoader();
        Class<?>[] interfaces = movable.getClass().getInterfaces();
        return Proxy.newProxyInstance(classLoader, interfaces, (o, method, args) -> {
            System.out.println("MyJdkProxy start!");
            long startTime = System.currentTimeMillis();
            Object object = method.invoke(movable, args);
            long endTime = System.currentTimeMillis();
            System.out.println("move time: " + (endTime - startTime));
            System.out.println("MyJdkProxy end!");
            return object;
        });
    }
}
